from application.model.ImageModel import ImageModel
from application.util.MysqlUtil import mysql_session


class ImageMapper:
    @classmethod
    def insert(cls, image_model: ImageModel) -> bool:
        """
        插入图片数据
        :param image_model: 图片数据模型
        :return:
        """
        try:
            mysql_session.add(image_model)
            mysql_session.commit()
            return True
        except Exception:
            mysql_session.rollback()
            return False

    @classmethod
    def get_info_by_ids(cls, image_ids: str) -> list:
        """
        根据图片ID列表，获取图片信息列表
        :param image_ids: 图片ID列表
        :return: 图片信息列表
        """
        results: list = mysql_session.query(ImageModel).filter(ImageModel.id.in_(image_ids.split(","))).all()
        return results

    @classmethod
    def delete_by_id(cls, image_model: ImageModel) -> bool:
        """
        根据ID删除图片
        :param image_model: 图片数据模型
        :return:
        """
        try:
            mysql_session.delete(instance=image_model)
            mysql_session.commit()
            return True
        except Exception:
            mysql_session.rollback()
            return False
